﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XM
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }



        private void Form1_Load(object sender, EventArgs e)
        {
	    // 获取数据库信息
	    int j = 0;
        string sql = "select * from zw";
        DataTable lsb = DBhleper.read(sql);
	    for (int i = 0; i < 50; i++)
	    {
	        Button btn = new Button();
	        if (i % 10 == 0 && i != 0)
	        {
	            j++;
	        }

	        // 动态生成座位，设置座位样式
	        btn.Name= string.Format("btn_{0}",i);
	        btn.Location = new Point(50 + 19 * (i - j * 10), 40 + j * 19);
            btn.FlatStyle = FlatStyle.Flat;
            btn.FlatAppearance.BorderSize = 0;
	        btn.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);
	        btn.Width = 30;
            btn.Height = 30; ;
            btn.ImageList = this.imageList1;
	        btn.ImageIndex = 0;

	        // 绑定button单击事件
            btn.Click += new System.EventHandler(this.button1_Click);	      
	        btn.Tag = 0;

            //获取数据库被选中的座位
            int pd=Convert.ToInt32( lsb.Rows[i][1]);
            if (pd==1)
            {
                btn.ImageIndex = 1;
                btn.Tag = 1;
            }
	        this.Controls.Add(btn);
	    }
            //this.webBrowser1.Url = new Uri("http://www.baidu.com");//指定url地址为百度首页
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //HtmlElement searchWords = webBrowser1.Document.All["kw"];//获取百度搜索的文本框
            //HtmlElement searchButton = webBrowser1.Document.All["su"];//获取百度搜索的按钮
            //searchWords.SetAttribute("value", "guwei4037");//给百度搜索的文本框赋值
            //searchButton.InvokeMember("click");//调用百度搜索按钮的点击事件


            // 绑定当前获取座位
            Button btn = (Button)sender;
            if (btn.Tag.Equals(0))
            {
                btn.ImageIndex = btn.ImageIndex == 0 ? 1 : 0;
            }
            int id = Convert.ToInt32(btn.Name.Substring(4));
            YZ.fm = id;
            if (id<=10)
            {
                label6.Text =string.Format( "第一排第{0}列",(id+1));
            }
            else if(id<=20)
            {
                label6.Text = string.Format("第二排第{0}列", (id + 1));
            }
            else if (id<=30)
            {
                label6.Text = string.Format("第三排第{0}列", (id + 1));
            }
            else if (id <=40)
            {
                label6.Text = string.Format("第四排第{0}列", (id + 1));
            }
            else
            {
                label6.Text = string.Format("第五排第{0}列", (id + 1));
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Button btn;
	    foreach (Control con in this.Controls)
	    {
        if (con.Name.StartsWith("btn"))
        {
            //获取改变的座位
            btn = (Button)con;
	            if (btn.Tag.Equals(0) && btn.ImageIndex == 1)
	            {
                    //截取被改变控件的名字的最后一位数
                    int id =Convert.ToInt32( btn.Name.Substring(4));
                    string sql = string.Format(" update zw set status = 1  where id = {0}",YZ.fm+1);
                    int fh = DBhleper.write(sql);
                    if (fh>0)
                    {
                        MessageBox.Show("购买成功");
                        this.Close();
                    }
	            }
	        }
	    }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
